<?php
/**
 * Created by PhpStorm.
 * User: haobin
 * Date: 2016/9/7
 * Time: 16:25
 */
include('dayu/top/TopClient.php'); //这个是你下面实例化的类
include('dayu/top/ResultSet.php'); //这个是topClient 里面需要实例化一个类所以我们也要加载 不然会报错
include('dayu/top/RequestCheckUtil.php'); //这个是成功后返回的信息文件
include('dayu/top/TopLogger.php');   //这个是错误信息返回的一个php文件
include('dayu/top/request/AlibabaAliqinFcSmsNumSendRequest.php'); //这个也是你下面示例的类*/
require('conn.php');
require('functions.php');
$lifeTime = 60;
session_set_cookie_params($lifeTime);
session_start();

//stdClass Object 转 数组
function objectArray($array)
{
    if (is_object($array)) {
        $array = (array)$array;
    }
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            $array[$key] = objectArray($value);
        }
    }
    return $array;
}

if ($_GET) {
    $phone = guolv($_GET['phone']);
    $ip = GetIP();
    $day = date("Y-m-d", time());
    //注册信息
    $ipTimes = $mysql->query("select COUNT(*) as iptimes from `register` WHERE  `ip`='{$ip}' AND `day`='{$day}'");
    //同一ip一天只能收此次验证码4次
    if($ipTimes[0]['iptimes'] >= 4){
        echo 2;
        exit;
    }
    $regInfo = $mysql->query("select * from `register` WHERE `phone`='{$phone}' AND `day`='{$day}'");
    if($regInfo){
        $times = $regInfo[0]['times'];
        if($times <= 0){
            echo 2;//操作太频繁
            exit;
        }
        $res = $mysql->execute("update `register` set `times`=`times`-1 WHERE `phone`='{$phone}' AND `day`='{$day}'");
    }else{
        $userInfo = [
            'phone' => $phone,
            'ip' => $ip,
            'day' => $day,
        ];
        $regSql = "insert into `register`" . arr2s($userInfo);
        $res = $mysql->execute($regSql);
    }
    if (!$_SESSION['yzm_time']) {
        $rand = rand(100000, 999999);
        $msg = "";
        $ym = "【有妹纸】";
        /*实例化大鱼接口*/
        $c = new TopClient;
        $c->appkey = "23352695";
        $c->secretKey = "b2b10433487dc7a27f6cc82aea034d1f";
        $req = new AlibabaAliqinFcSmsNumSendRequest;
        $req->setExtend("123456");
        $req->setSmsType("normal");
        $req->setSmsFreeSignName("注册验证");
        $req->setSmsParam("{\"code\":\"$rand\",\"product\":\"$ym\"}");
        $req->setRecNum("$phone");
        $req->setSmsTemplateCode("SMS_8220695");

        //执行
        $resp = $c->execute($req);
        //大鱼返回值
        $res = objectArray($resp);
        if ($res['result']['success']) {
            setcookie("phone", $phone, time() + 300);
            setcookie("yzm", md5($rand), time() + 300);
            $_SESSION['yzm_time'] = "300";
            echo 1;//验证码发送成功
        } else {
            echo 2;//用户操作过于频繁,验证码发送失败
        }
    } else {
        echo 3;//再次发送验证码时间
    }
}
if ($_POST) {
    $day = date("Y-m-d", time());
    $ip = GetIP();
    //注册
    $phone = guolv($_POST['phone']);
    $password = guolv($_POST['password']);
    $yzm = guolv($_POST['yzm_value']);
    //相同ip不能多次注册
//    if ($config['ipreg2'] == 1) {
//        $row_ip = $mysql->query("select count(*) as c  from `userdata` where `ip`='{$ip}' and day='{$day}' order by `id` limit 1");//获取相同ip
//        if ($row_ip[0]['c' > 5]) {
//            echo "<script>alert('本ip已经注册太多啦!');location.href='regs.php?uid=" . $tj_id . "'</script>";
//            exit;
//        }
//    }

    if (md5($yzm) == guolv($_COOKIE['yzm']) && $phone == guolv($_COOKIE['phone'])) {
        $sql = "update  `user` set `password` = {$password} WHERE `phone` = {$phone}";
        $row = $mysql->execute($sql);
        if ($row) {
            //修改成功
            echo 1;
        } else {
            echo 2;//修改失败
        }
    } else {
        echo 0;//信息验证失败
    }
}